package com.rd.student.jiaji.class4.suyan.zrrd.day29_0731;

import java.util.*;

public class TestRandom {
  private static   String all="郑成祥、王博、孙文卓、郑睿娴、王高博、李春泉、张睿、王星凯、柏云耀、高艺铭、周子贺、杜航、高晨曦、张思韬、孙昊、方周、郭佳宝、姜万里、李宏鸣、薛志鑫、王海鑫、夏银银、吴俊豪、王向阳、徐铭泽、张世煊、才雨鑫、孙国伟、秦嘉林、王岩博、石睿、杜雨坤、黄奕鸣、金洪宇、李鑫、王宇芳、苏岩";
   private static String[] allArr= all.split("、");
    public static void main(String[] args) {

        System.out.println(allArr.length);
        for (int i=0;i< allArr.length;i++){
            System.out.println(allArr[i]);
        }
//        Scanner scanner=new Scanner(System.in);
//        System.out.println("请输入要随机的人数");
//        int n=scanner.nextInt();
//        int len=allArr.length;
//        for (int i=0;i<n;i++){
//            int index=(int)(Math.random()*len);
//            System.out.println(allArr[index]);
//        }
//        SetTest();
//        TestArr();
        ListTest();
    }
    //Set方法去重
    public static void SetTest(){
        Set set=new HashSet();
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要随机的人数");
        int n=scanner.nextInt();
        while (set.size()<n){
            int randoms=(int)(Math.random()* allArr.length);
            set.add(randoms);
        }
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(allArr[(int)iterator.next()]);
        }
    }
    //数组方法去重
    public static void TestArr(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要随机的人数");
        int n=scanner.nextInt();
        int[] indexArr1=new int[allArr.length];
        for (int i=0;i<indexArr1.length;i++){
         indexArr1[i]=-1;
        }
        System.out.println(Arrays.toString(indexArr1));
        for (int i=0;i<n;i++){
         int random=(int)(Math.random()* allArr.length);
         if (indexArr1[random]==-1){
             //代表random没有出现过
             indexArr1[random]=random;
             System.out.println("随机出现"+random);
             System.out.println(allArr[random]);
             System.out.println(Arrays.toString(indexArr1));
         }
         else {
             //代表已经出现过了 这一次随机不生效
             System.out.println("已经随机过了"+random);
             i--;
         }
        }
    }
    //List方法去重
    public static void ListTest(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要随机的个数");
        int n=scanner.nextInt();
        List<Integer> list=new ArrayList<>(n);
        for (int i=0;i<n;i++){
            int random=(int)(Math.random()* allArr.length);
            //要么将random存入 要么将random抛弃
            if (list.contains(random)){
               //如果已经随机出现过
                System.out.println("已经随机过了"+random);
                i--;
                continue;
            }
            list.add(random);
            System.out.println(allArr[random]);
        }
        System.out.println(list);
    }
    //使用List.size()方法来和随机生成的n来进行比较
    public static void ListSizeTest(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要随机的个数");
        int n=scanner.nextInt();
        List<String> list=new ArrayList<>(n);
        while (list.size()<n){
            int random=(int)(Math.random()* allArr.length);
            String ele=allArr[random];
            if (!list.contains(ele)){
                list.add(ele);
            }
            System.out.println(list);
        }
    }
}
